package http

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/paginate"
	"gitee.com/captials-team/ubdframe/src/domain/dto/reqdata"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/pkg/gin_http"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/gin-gonic/gin"
	"go.uber.org/dig"
)

type TelemetryController struct {
	l    v1log.ILog
	conf *configstc.IotAppConfig
	srv  interfaces.ItfTelemetryService

	gin_http.ResponseController
}

func NewTelemetryController(di *dig.Container, l v1log.ILog, conf *configstc.IotAppConfig, srv interfaces.ItfTelemetryService) *TelemetryController {
	ctr := &TelemetryController{
		l:    l,
		conf: conf,
		srv:  srv,
	}

	return ctr
}

// GetLatest godoc
// @Summary      获取测点最新数据
// @Description  获取测点最新数据
// @Tags         user
// @Produce      json
// @Param        param    body     reqdata.MetricDataReq  true  "查询参数"
// @fail 1 {object} respdata.ResponseData{} "重置失败"
// @success 200  {object} respdata.ResponseData{data=models.MetricData} "重置成功"
// @Router       /mag/telemetry/latest [post]
func (ctr *TelemetryController) GetLatest(ctx *gin.Context) {
	var req reqdata.MetricDataReq
	ctx.ShouldBind(&req)

	data, err := ctr.srv.QueryLatestData(req.MetricId)
	if err != nil {
		ctr.l.Error("search err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(data))
}

// SearchHistory godoc
// @Summary      测点历史数据
// @Description  测点历史数据
// @Tags         user
// @Produce      json
// @Security     ApiKeyAuth
// @Param        param    body reqdata.SearchMetricDataReq  true  "查询参数"
// @success 200  {object} respdata.SearchListResponse{data=[]models.MetricData} "绑定成功"
// @success 201 {object} models.MetricData{} "具体子项数据"
// @Router       /mag/telemetry/history [post]
func (ctr *TelemetryController) SearchHistory(ctx *gin.Context) {
	var req reqdata.SearchMetricDataReq
	ctx.ShouldBind(&req)

	list, pa, err := ctr.srv.SearchHistoryData(&dto.SearchMetricDataDto{
		MetricId:  req.MetricId,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
	}, paginate.NewPager(req.Page, req.Size))
	if err != nil {
		ctr.l.Error("search err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(respdata.SearchListResponse{
		List:     list,
		Paginate: *pa,
	}))
}

// SearchStats godoc
// @Summary      测点统计数据
// @Description  测点统计数据
// @Tags         user
// @Produce      json
// @Security     ApiKeyAuth
// @Param        param    body reqdata.SearchMetricStatReq  true  "查询参数"
// @success 200  {object} respdata.SearchListResponse{data=[]models.MetricStatValueData} "绑定成功"
// @success 201 {object} models.MetricData{} "具体子项数据"
// @Router       /mag/telemetry/stats [post]
func (ctr *TelemetryController) SearchStats(ctx *gin.Context) {
	var req reqdata.SearchMetricStatReq
	ctx.ShouldBind(&req)

	list, pa, err := ctr.srv.SearchStatData(&dto.SearchMetricStatDto{
		MetricId:  req.MetricId,
		StatType:  req.StatType,
		DataType:  req.DataType,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
	}, paginate.NewPager(req.Page, req.Size))
	if err != nil {
		ctr.l.Error("search err %s", err)
		ctr.Response(ctx, respdata.CSysBusy)
		return
	}

	ctr.Response(ctx, respdata.CSuccess.MData(respdata.SearchListResponse{
		List:     list,
		Paginate: *pa,
	}))
}
